<?php

require('config.php');
$language_link=$_GET['id'];
if($language_link != null && $language_link > 0 ) {
	$real_estate = modelRealEstate::getObject($language_link);
	if(!$real_estate)redirectFront(index);
}else{
	redirectFront(index);
}

if(isset($_GET['title_debug']) && trim($_GET['title_debug'])!=''){
	$_SESSION['title_debug'] = $_GET['title_debug'];
	$title_debug = $_GET['title_debug'];
}
else {
	$title_debug='';
	unset($_SESSION['title_debug']);
}

$page = new rootLayoutPage();
$page->addStyleSheet('table_sorter.css');
$page->addJavaScript('jQuery.pagination.js');
$page->addStyleSheet('pagination.css');

$page->setPageSameLevel($real_estate->id_menu_left_tree);

//-- Menu Page ----------------------------------------------//
$menu = new layoutMenuLeft();
	$menuPage = new layoutMenuPage();
	$menuPage->setTable('real_estate_type');
	$menuPage->setIdShow($real_estate->id_menu_left_tree);
	$menu->setIdShow($real_estate->id_menu_left_tree);
	$menu->pushMenu($menuPage);

//--------Menu indicator--------------//
$menuIndictor = new layoutMenuIndicator();
	$menuIndictor->idTree($real_estate->id_menu_left_tree);
	$menuIndictor->title($real_estate->title);
	$page->setMenuIndicator($menuIndictor);

//************************//
$arr = array();
$getListUrlRealEstate = modelRealEstate::getListUrlRealEstate($language_link);
foreach($getListUrlRealEstate AS $key=>$Url){
	$arr[$Url->language_code]=$Url->url_rewriting.'-'.$Url->language_link.'.html';
}
$header = new layoutHeader();
$header->setArrayUrl($arr);
$header->setIdShow($real_estate->id_menu_left_tree);
$page->setHeader($header);

$page->setMenuPage($menu);
$page->setBaliseTitle($real_estate->meta_title);
$page->setDescription($real_estate->meta_description);
$extension = filename_extension($_SERVER['REQUEST_URI']);
$page->setCanonical($html_name);

$xml = tmxTranslate('layoutFooter.xml', REP_XLAT_LAYOUT);

$tpl_file = 'real_estate.tpl';
$Body = new flyLayout(REP_TPL . $tpl_file);
$Body->start();

	$Body->setVariable('text_price',$xml['text_price_sell']);
	$Body->setVariable('text_type',$xml['text_type']);
	
	$Body->setVariable('text_province',$xml['text_province']);
	//$Body->setVariable('text_city',$xml['text_city']);
	
	$Body->setVariable('page_title',$real_estate->title);
	//****Pagination***********//

	if(isset($_GET["page"]) > 1) {
		$id_page = $_GET['page'];
	    $limit = (($id_page-1)*ITEM_PER_PAGE_REAL_ESTATE)." , ".ITEM_PER_PAGE_REAL_ESTATE;;
	}else{
		$id_page = '';
	    $limit = '0 , '.ITEM_PER_PAGE_REAL_ESTATE;
	}
	$format = modelUploadFormat::getObject(FORMAT_PHOTO_LIST);
	$format_zoom = modelUploadFormat::getObject(FORMAT_PREVIEW_PHOTO_LIST);
	/*$listLocationAll = modelRealEstate::getListLocationByID($language_link,$limit, $title_debug,null,$_GET['id_category']);
	$countlistLocation = modelRealEstate::countgetListLocationByID($language_link,$limit, $title_debug,null,$_GET['id_category']);*/
	$listLocationAll = modelRealEstate::getListLocationByID($language_link,$limit, $title_debug,null);
	$countlistLocation = modelRealEstate::countgetListLocationByID($language_link,$limit, $title_debug,null);
	//echo $listLocationAll->size().'='.$countlistLocation->countall;
	if($listLocationAll->size() > 0){
		foreach($listLocationAll as $key => $location){
			if($location->url_repertoire){
				if(file_exists($location->url_repertoire.$format->url_format.$location->nom_serveur) and isImageFileName($location->nom_serveur)){
					$fichier = modelUploadFile::getObject($location->id_fichier);
					list($width, $height, $type, $attr) = getimagesize($fichier->url_repertoire.$format->url_format.$fichier->nom_serveur);
					$url_image = $fichier->url_repertoire.$format->url_format.$fichier->nom_serveur;
					//$url_image =$fichier->url_repertoire.$format_zoom->url_format.$fichier->nom_serveur;
					if(file_exists($location->url_repertoire.$format_zoom->url_format.$location->nom_serveur) and isImageFileName($location->nom_serveur)){
						$url_image_zoom = $fichier->url_repertoire.$format_zoom->url_format.$fichier->nom_serveur;
					}else{
						$url_image_zoom = REP_DESIGN.'noimage.jpg';
					}
					//$img = '<img data-original="/'.$url_image.'" align="left" class="imagePre" />';
					$img = '<img src="/'.$url_image.'" width="'.$width.'px" height="'.$height.'px" class="imagePre" />';
				}else{
					$img='';
				}
			}else{
				$img='';
			}
			$Body->setVariable('photo',$img);
			
			$comma = array();
			$i = 0;
			if($location->length>0){
				$comma[$i] = $xml['label_length'].' '.$location->length.$xml['label_meter'] ;$i=$i+1;
			}
			if($location->width>0){
				$comma[$i] = $xml['label_width'].' '.$location->width.$xml['label_meter'] ;$i=$i+1;
			}
			if($location->size_building>0){
				$comma[$i] = $xml['label_area'].' '.$location->size_building.$xml['label_meter_square'] ;$i=$i+1;
			}
			if($location->length_land>0){
				$comma[$i] = $xml['label_land_length'].' '.$location->length_land.$xml['label_meter'] ;$i=$i+1;
			}
			if($location->width_land>0){
				$comma[$i] = $xml['label_land_width'].' '.$location->width_land.$xml['label_meter']  ;$i=$i+1;
			}
			if($location->size_land>0){
				$comma[$i] = $xml['label_land_area'].' '.$location->size_land.$xml['label_meter_square'] ;$i=$i+1;
			}
			if($location->bedrooms>0){
				$comma[$i] = $xml['label_bedrooms'].' '.$location->bedrooms ;$i=$i+1;
			}
			if($location->bathrooms>0){
				$comma[$i] = $xml['label_bathrooms'].' '.$location->bathrooms ;$i=$i+1;
			}
			$Body->setVariable('id_addr',$key);
			$count = count($comma);
			$string = '' ;
			foreach($comma AS $key => $c){
				 $count==$key+1?$string.=$c:$string.=$c.', ';
			}
			$Body->setVariable('place_description',$string);
			

			$Body->setVariable('meta_title',$location->meta_title);
			$Body->setVariable('place_name',$location->name_place);
			$Body->setVariable('map_address',nl2br($location->address));
			

			$Body->setVariable('place_reference',$location->reference);
			//$Body->setVariable('city_meta_title',$location->city_meta_title);
			$Body->setVariable('district',$location->name_district);
			$Body->setVariable('province',$location->name_province);
			$Body->setVariable('province_meta_title',$location->province_meta_title);

			$Body->setVariable('price_sell',$location->price_sell<>0?number_format($location->price_sell,0,'.',' ').' '.CURRENCY:'');
			$Body->setVariable('price_rental',$location->price_rental<>0?number_format($location->price_rental,0,'.',' ').' '.CURRENCY:'');

			$Body->setVariable('url_category_map', $location->url_rewriting.'-'.$location->language_link.'.html');
			$Body->parseList('list_category_map');


		}
		$Body->showBlock('real_estate');
	}else{
		$Body->showBlock('nohting');
	}
	$arrayTpl = array();
	$arrayPagination = array();
	$arrayTpl[1]='pagination_search_top';
	$arrayTpl[2]='pagination_search_bottom';
	for($i=1;$i<=2;$i++){
		if (file_exists(REP_TPL . $_SESSION['rep_language'] . $arrayTpl[$i].'.tpl')){
		$arrayPagination[$i] = new flyLayout(REP_TPL . $arrayTpl[$i].'.tpl');
		}
		else{$arrayPagination[$i] = new flyLayout(REP_TPL . $arrayTpl[$i].'.tpl');
		}
		$arrayPagination[$i]->start();
		$arrayPagination[$i]->setVariable('rep_ajax', REP_AJAX.'product/');
		$arrayPagination[$i]->setVariable('rep_item_lister', 'list_search_real_estate.php?lang='.$_SESSION['language_code'].'&id='.$language_link);
		$i==1?$arrayPagination[$i]->setVariable('rep_item_counter', 'list-real-estate-search-count-item.php?lang='.$_SESSION['language_code'].'&id='.$language_link):'';
		$i==1?$arrayPagination[$i]->setVariable('page',  $id_page):'';
		$i==1?$arrayPagination[$i]->setVariable('item_per_page', ITEM_PER_PAGE_REAL_ESTATE):'';
		$i==1?$arrayPagination[$i]->setVariable('num_display_entries', NUM_DISPLAY_ENTRIES):'';
		$i==1?$arrayPagination[$i]->setVariable('delay_search', DELAY_SEARCH):'';
		$i==1?$arrayPagination[$i]->setVariable('delay_fifty_mili_second', DELAY_FIFTY_MILI_SECOND):'';
		$i==1?$arrayPagination[$i]->setVariable('default_text_search', isset($_SESSION['title_debug'])?$_SESSION['title_debug']:''):'';
		$i==1?$arrayPagination[$i]->setVariable('num_items', $countlistLocation->countall):'';

		$arrayPagination[$i]->showBlock('pagination_search');
		$arrayPagination[$i]->stop();
	}
    //$Body->includeFile('pagination_search_up', $arrayPagination[1]);
    //$Body->includeFile('pagination_search_down', $arrayPagination[2]);

//******Rented and sold****//
$Body->stop();
$page->setBody($Body);
$page->display();

?>